<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm"
               @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="24">
          <el-form-item :label="$t('sysApp.appId')" prop="appId">
            <!--文本框-->
            <el-input v-model="dataForm.appId" :placeholder="$t('sysApp.appId')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('sysApp.appName')" prop="appName">
            <!--文本框-->
            <el-input v-model="dataForm.appName" :placeholder="$t('sysApp.appName')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="appType" :label="$t('sysApp.appType')" size="mini">
            <el-radio-group v-model="dataForm.appType">
              <!-- <el-radio :label="1">{{ $t('sysApp.appType1') }}</el-radio> -->
              <el-radio :label="'0'">{{ $t('sysApp.appType0') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 接口URL -->
          <el-form-item label="接口统一URL" prop="apiUrl">
            <!--文本框-->
            <el-input v-model="dataForm.apiUrl" placeholder="接口统一URL" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 接口访问AK -->
          <el-form-item label="访问AccessKey" prop="apiAccessKey">
            <!--文本框-->
            <el-input v-model="dataForm.apiAccessKey" placeholder="访问AccessKey" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 接口访问SK -->
          <el-form-item label="访问SecretKey" prop="apiSecretKey">
            <!--文本框-->
            <el-input v-model="dataForm.apiSecretKey" placeholder="访问SecretKey" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 接口访问扩展参数json -->
          <el-form-item label="扩展参数" prop="extendJson">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.extendJson" placeholder="扩展参数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('sysApp.appContacts')" prop="appContacts">
            <!--文本框-->
            <el-input v-model="dataForm.appContacts" :placeholder="$t('sysApp.appContacts')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('sysApp.appPhone')" prop="appPhone">
            <!--文本框-->
            <el-input v-model="dataForm.appPhone" :placeholder="$t('sysApp.appPhone')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('sysApp.appMail')" prop="appMail">
            <!--文本框-->
            <el-input v-model="dataForm.appMail" :placeholder="$t('sysApp.appMail')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('sysApp.appAddress')" prop="appAddress">
            <!--文本框-->
            <el-input v-model="dataForm.appAddress" :placeholder="$t('sysApp.appAddress')" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        appId: '',
        appName: '',
        appType: '0',
        appContacts: '',
        appPhone: '',
        appMail: '',
        appAddress: '',
        apiAccessKey: '',
        apiSecretKey: '',
        extendJson: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        appId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        appName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        appType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/sys/sysApp/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        console.log(this.dataForm)
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/sysApp/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
